﻿Ctrl = {
	img8 : "<img src='/images/BacoError_wev8.gif' align='absMiddle'>",
	checked : function(o){
		if (undefined == o) return;
		jQuery(o).attr("checked","checked");
	},
	inputEach : function (id,f){
		var o = jQuery("input[id^='"+id+"']");
		if(o.length){
			jQuery.each(o,function( i,e ){
				var indexRow = jQuery(e).attr("id").substring(id.length);
				var value = jQuery(e).val();
				if(f) f( indexRow,i,value );
			});
		}
	},
	selectEach : function (id,f){
		var o = jQuery("select[id^='"+id+"']");
		if(o.length){
			jQuery.each(o,function( i,e ){
				var indexRow = jQuery(e).attr("id").substring(id.length);
				var value = jQuery(e).val();
				if(f) f( indexRow,i,value );
			});
		}
	},
	stEach : function (id){
		var values = new Array();
		var o = jQuery('input[id^="' + id + '"]');
		if (! o.length) o = jQuery('select[id^="' + id + '"]');
		jQuery(o).each(function ( i,e ){
			var v = jQuery.trim(jQuery(e).val());
			if( v!="" ){
				values.push(v);
			}
		});
		return values;
	},
	bindChange : function (id,f){
		var o = jQuery("#"+id);
		if(o){
			var type = o.attr("type");
			if("hidden"==type){
				o.bindPropertyChange(function(){
					f();
				});
			}else{
				jQuery("#"+id).change(function(){
					f();
				});
			}
		}
	},
	v : function(id,value){
		jQuery("#"+id).val(value);
		jQuery("#"+id).change();
		jQuery("#"+id).blur();
	},
	vt : function(id,value,html){
		jQuery("#"+id).val(value);
		if(html){
			jQuery("#"+id+"span").html(html);
		}else{
			jQuery("#"+id+"span").html(value);
		}
		jQuery("#"+id).change();
		jQuery("#"+id).blur();
	},
	vmt : function(id,value,isMain){
		if(isMain) {
			jQuery("#"+id).val(value);
			jQuery("#"+id+"_span").text(value);
			jQuery("#"+id).change();
			jQuery("#"+id).blur();
		}else {
			jQuery("#"+id+"_d").val(value);
			jQuery("#"+id+"_span_d").text(value);
			jQuery("#"+id+"_d").change();
			jQuery("#"+id+"_d").blur();
		}
		
	},
	vhrm : function (id,ismast,value){
		_c = this;
		var hrmInfo = _c.hrmInfo(value);
		var span = "";
		for(var x in hrmInfo){
			var o = hrmInfo[x];
			if(String(o.id)!="undefined"){
				span += wrapshowhtml0(1,"<a href='javaScript:openhrm("+o.id+");' onclick='pointerXY(event);'>"+o.lastname+"</a>&nbsp", o.id);
			}
		}
		jQuery("#"+id).val(value);
		jQuery("#"+id+"span").html(span);
		jQuery("#"+id).change();
		jQuery("#"+id).blur();
		//__browserNamespace__._writeBackData(id,false,{id:value,name:"<a href='javascript:openhrm("+value+")'>"+text+"</a>"});
	},
	vbrowser : function (id,ismast,value,text){
		jQuery("#"+id).val(value);
		var span = wrapshowhtml0(1,"<a title='" + value + "'>" + text + "</a>&nbsp", value);
		jQuery($GetEle(id+"span")).html(span);
		//jQuery("#"+id+"span").html(span);
		jQuery("#"+id).change();
		jQuery("#"+id).blur();
		if(String(value)=="undefined" || String(value)=="null" || value==""){
			jQuery($GetEle(id+"span")).html("");
			//jQuery("#"+id+"spanimg").html(this.img8);
		}else{
			jQuery("#"+id+"spanimg").text("");
		}
		//__browserNamespace__._writeBackData(id,false,{id:value,name:"<a href='#'>"+text+"</a>"});
	},
	vbrowsers : function(id,ismast,value,text,linkurl){
		var resourceIdArray = value.split(",");
		var resourceNameArray = text.split(",");
		var sHtml = "";
		if(!linkurl)
			linkurl = "/workflow/request/ViewRequest.jsp?isrequest=1&requestid=";
		for (var _i=0; _i<resourceIdArray.length; _i++) {
			var curid = resourceIdArray[_i];
			var curname = resourceNameArray[_i];
			if(curid!=""){
				if (linkurl == "/hrm/resource/HrmResource.jsp?id=") {
					sHtml += wrapshowhtml0($G(id).getAttribute("viewtype"), "<a href=javaScript:openhrm(" + curid + "); onclick='pointerXY(event);'>" + curname + "</a>&nbsp", curid);
				} else if(linkurl.indexOf("/hrm/resource/HrmResource.jsp") > -1){
					sHtml += wrapshowhtml0($G(id).getAttribute("viewtype"), "<a href=javaScript:openhrm(" + curid + "); onclick='pointerXY(event);'>" + curname + "</a>&nbsp", curid);
				} else {
					sHtml += wrapshowhtml0($G(id).getAttribute("viewtype"), "<a href=" + linkurl + curid + "&f_weaver_belongto_userid="+window.__userid+"&f_weaver_belongto_usertype="+ window.__usertype + " target=_blank>" + curname + "</a>&nbsp", curid);
				}
			}
		}

		jQuery($GetEle(id+"span")).html(sHtml);
		$GetEle(id).value= value;
		hoverShowNameSpan(".e8_showNameClass");

	},
	readonly :function(id){ 
		jQuery("#"+id).attr("readonly","readonly"); 
	},
	edit :function(id){ 
		jQuery("#"+id).removeAttr("readonly");
	},
	addRow : function ( detailsIndex, f ){
		if (undefined == detailsIndex) return;
		var addmethod = "addRow" + detailsIndex + "(" + detailsIndex + ")";
		try{
			eval(addmethod);
			var n = jQuery("#indexnum" + detailsIndex).val()* 1.0 - 1;
			if(f) f(n);
		}catch (e) {
			console.log(e);
		}
	},
	deleteRow : function ( detailsIndex, rowIndex ){
		if (undefined == detailsIndex) return;
		var cs = document.getElementsByName('check_mode_' + detailsIndex);
		if (cs.length < 1) return;
		jQuery("input[name='check_mode_" + detailsIndex+"']").each(function(index,element){ 
			if(jQuery(element).val() == rowIndex) jQuery(element).attr("checked","checked");
		}); 
		var oldIsdel = window.isdel;
		window.isdel = function(){return true;};
		eval("deleteRow" + detailsIndex + "(" + detailsIndex + ")");
		window.isdel = oldIsdel;
	},
	deleteAllRow : function(detailsIndex){
		_c = this;
		if (undefined == detailsIndex) return;
		_c.addRow(detailsIndex);
		jQuery("input[name='check_node_" + detailsIndex+"']").each(function(index,element){ 
			jQuery(element).attr("checked","checked");
		}); 
		var oldIsdel = window.isdel;
		window.isdel = function(){return true;};
		eval("deleteRow" + detailsIndex + "(" + detailsIndex + ")");
		window.isdel = oldIsdel;
	},
	setButten : function(id,text,f){
		var btnId = "btn_"+id;
		jQuery("#"+id).append("<input id=\""+btnId+"\" class=\"e8_btn_top_first\" style=\"max-width: 100px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;\" type=\"button\" value=\""+text+"\"/>");
		jQuery("#"+btnId).click(function (){
			f();
		});
	},
	openDialog : function(url,title,width,height,f){
		var dlg = new window.top.Dialog();	//定义Dialog对象
		dlg.currentWindow = window;
		dlg.Model=true;
		dlg.maxiumnable=true;
		dlg.Width=width;
		dlg.Height=height;
		dlg.URL="/systeminfo/BrowserMain.jsp?url="+url;
		dlg.Title=title;
		dlg.callbackfun = function(paramobj, id1, otherdata){
			if(f){
				var returnObj = paramobj;
				if(returnObj==null)
					returnObj = id1;
				f(returnObj);
			}
		};
		dlg.CancelEvent = function(){
			dlg.close();
		};
		dlg.show();
	},
	open : function(url,width,height){
		var r = window.top.showModalDialog(url , undefined
				,'dialogHeight:' + height + 'px;'
				+ 'dialogWidth:' + width + 'px;'
				+ 'center:yes;menubar:no');
		return r;
	},
	ajax : function(url,data,dataType){
		var datas = "";
		jQuery.ajax({
			type: "POST",
			url: url,
			data: data,
			dataType: dataType,
			async: false,
			success : function (data){
				datas = data;
			}
		});
		return datas;
	},
	ajaxJson : function(url,data){
		var datas;
		jQuery.ajax({
			type: "POST",
			url: url,
			data: data,
			dataType: "json",
			async: false,
			success : function (data){
				if(data){
					if( data.flag ){
						displayAllmenu();
						datas = data.result;
					}else{
						enableAllmenu();
						window.top.Dialog.alert(data.msg);
					}
				}else{
					window.top.Dialog.alert("callback is null!");
				}
			}
		});
		return datas;
	},
	hrmInfo : function(userId){
		_c = this;
		var json = _c.ajax("/westvalley/common/hrmPerson.jsp", {id:userId}, "json");
		return json;
	},
	setReadonly : function(id){	//设置只读
		var o = jQuery("#"+id);
		if(o){
			var type = o.attr("type");
			if("hidden"!=type){
				var span = jQuery("#"+id+"span");
				span.html(o.val());
				o.hide();
			}
		}
	},
	setMandatory : function(id){	//设置必填
		var span = jQuery("#"+id+"span");
		if(span.html()==""){
			span.html("<img src='/images/BacoError_wev8.gif' align='absmiddle'>");
		}
		var inputcheck = jQuery("input[name='inputcheck']");
		var v = inputcheck.val();
		if(v==""){
			inputcheck.val(id);
		}else{
			inputcheck.val(v+","+id);
		}
	},
	checkDateTime : function(v){
		var reg = /^(\d+)-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/; 
	    var r = v.match(reg); 
	    if(r==null)return false; 
	    r[2]=r[2]-1; 
	    var d= new Date(r[1], r[2],r[3], r[4],r[5], r[6]); 
	    if(d.getFullYear()!=r[1])return false; 
	    if(d.getMonth()!=r[2])return false; 
	    if(d.getDate()!=r[3])return false; 
	    if(d.getHours()!=r[4])return false; 
	    if(d.getMinutes()!=r[5])return false; 
	    if(d.getSeconds()!=r[6])return false; 
	    return true;
	}
};

DataFormat = {
	null2String : function (obj){
		if(String(obj)=="undefined" || String(obj)=="null" ){
			return "";
		}else{
			return ""+obj;
		}
	},
	getString : function (v){
		try{
			return v.toString();
		} catch(e){
			return "";
		}
	},
	getNum : function (v){
		var _f = this;
		if( _f.null2String(v)=="" ) return 0.0;
		try{
			return parseFloat(v);
		} catch(e){
			return 0;
		}
	},
	getInt : function (v){
		var _f = this;
		if( _f.null2String(v)=="" ) return 0;
		try{
			return parseInt(v);
		} catch(e){
			return 0;
		}
	},
	getDate : function (v){
		return new Date(v.replace(/-/g,"/")); 
	},
	arr2String : function(arr,split){
		var map = new Map();
		var str = "";
		for(var x in arr){
			var temp = map.get(arr[x]);
			if( !temp ){
				map.put(arr[x], arr[x]);
				str += split+arr[x];
			}
		}
		return str;
	},
	str2Arr : function (str,split){
		_f = this;
		var temp = new Array();
		var arr = str.split(split);
		for( var i=0;i<arr.length;i++ ){
			if( arr[i] != "" ){
				temp.push(arr[i]);
			}
		}
		return temp;
	},
	compareDate: function (a, b){
		var arr = a.split("-");
	    var starttime = new Date(arr[0], arr[1], arr[2]);
	    var starttimes = starttime.getTime();

	    var arrs = b.split("-");
	    var lktime = new Date(arrs[0], arrs[1], arrs[2]);
	    var lktimes = lktime.getTime();
	    if (starttimes > lktimes) {
	        return false;
	    }
	    else
	        return true;
	},
	weekInfoDay : function(sDate,eDate){
		var _f = this;
		var weekendDays = 0;	//周末天数
	    var normalDays = 0;		//工作日天数
	    if( _f.null2String(sDate)!="" && _f.null2String(eDate)!="" ){
	    	var dStart = _f.getDate(sDate);
	        var dEnd = _f.getDate(eDate);
	        var dayMilliseconds = 1000 * 60 * 60 * 24;	//一天对应的毫秒数

	        while (dStart.getTime() <= dEnd.getTime()) {
	            var day = dStart.getDay();
	            if (day === 0 || day === 6) {
	                weekendDays++;
	            }
	            else{
	                normalDays++;
	            }
	            dStart = new Date(dStart.getTime() + dayMilliseconds);
	        }
	    }
	    return {
	    	weekendDays : weekendDays,	//周末天数
	    	normalDays : normalDays,	//工作日天数
	    	days : _f.getInt(weekendDays) + _f.getInt(normalDays) 	//区间天数
	    };
	}
};


//---------------------------------------------------  
//日期格式化  
//格式 YYYY/yyyy/YY/yy 表示年份  
//MM/M 月份  
//W/w 星期  
//dd/DD/d/D 日期  
//hh/HH/h/H 时间  
//mm/m 分钟  
//ss/SS/s/S 秒  
//---------------------------------------------------  
Date.prototype.Format = function(formatStr)   {   
	var str = formatStr;   
	var Week = ['日','一','二','三','四','五','六'];  

	str=str.replace(/yyyy|YYYY/,this.getFullYear());   
	str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100));   

	str=str.replace(/MM/,(this.getMonth()+1)>9?(this.getMonth()+1).toString():'0' + (this.getMonth()+1));   
	str=str.replace(/M/g,(this.getMonth()+1));   

	str=str.replace(/w|W/g,Week[this.getDay()]);   

	str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate());   
	str=str.replace(/d|D/g,this.getDate());   

	str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0' + this.getHours());   
	str=str.replace(/h|H/g,this.getHours());   
	str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0' + this.getMinutes());   
	str=str.replace(/m/g,this.getMinutes());   

	str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0' + this.getSeconds());   
	str=str.replace(/s|S/g,this.getSeconds());   
	return str;   
};

jQuery.fn.serializeObject = function()    
{    
   var o = {};    
   var a = this.serializeArray();    
   jQuery.each(a, function() {    
       if (o[this.name]) {    
           if (!o[this.name].push) {    
               o[this.name] = [o[this.name]];    
           }    
           o[this.name].push(this.value || '');    
       } else {    
           o[this.name] = this.value || '';    
       }    
   });    
   return o;    
};  

var _C = Ctrl;
var _F = DataFormat;
var _Z = null;